import pymysql


def connect():
    connection = pymysql.connect(
        host='192.168.238.101',
        port=3306,
        user='root',
        password='root',
        database='db_tuijian',
        charset='utf8mb4',
        cursorclass=pymysql.cursors.DictCursor
    )
    return connection


def execute_query(sql, params=None):
    with connect() as connection:
        with connection.cursor() as cursor:
            cursor.execute(sql, params)
            result = cursor.fetchall()
    return result


def execute_command(sql, params=None):
    with connect() as connection:
        with connection.cursor() as cursor:
            cursor.execute(sql, params)
            connection.commit()
